package org.liaohailong.helloworld;

import org.junit.Test;
import org.liaohailong.helloworld.example.TX;
import org.liaohailong.helloworld.hash.HashSolution;
import org.liaohailong.helloworld.hash.MyHashSet;
import org.liaohailong.helloworld.linkedlist.LinkedListSolution;
import org.liaohailong.helloworld.stack.Conclusion;
import org.liaohailong.helloworld.stack.Solution1;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Author: liaohailong
 * Time: 2021/5/12 09:46
 * Describe:
 */
public class Exam1 implements AUtil.ILog {

    @Test
    public void removeNegative() {
        AUtil.setLog(this);

        TX tx = new TX();
        int[] arr = tx.hello(); // 100W个数

        AUtil.printCostInMs(() -> {
            int n = tx.tableSizeFor(arr.length);
            Integer[] out = new Integer[n];
            Integer[] shuffle3 = tx.shuffle3(arr, out);
            AUtil.print100(shuffle3);
            AUtil.print100(out);
            long start = System.currentTimeMillis();
            LinkedList<Integer> list = new LinkedList<>();
            for (Integer integer : out) {
                if (integer != null) list.add(integer);
            }
            long cost = System.currentTimeMillis() - start;
            log("cost = " + cost + "ms");
            AUtil.print100(list);
        }, "hash map way to shuffle");
    }

    @Test
    public void oddEvenList() {
        LinkedListSolution l = new LinkedListSolution();
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
        l.oddEvenList(l.create(arr));
    }


    @Test
    public void isPalindrome() {
        LinkedListSolution l = new LinkedListSolution();
        int[] arr = {1, 2, 3, 3, 2, 1};
//        int[] arr = {1, 2, 3, 0, 3, 2, 1};
//        int[] arr = {0, 0};
//        int[] arr = {1, 0, 1};
//        int[] arr = {1, 2, 4, 0, 3, 2, 1};
        boolean palindrome = l.isPalindrome(l.create(arr));
        log("palindrome = " + palindrome);
    }

    @Test
    public void mergeTwoLists() {
        LinkedListSolution l = new LinkedListSolution();
//        int[] arr1 = {1, 2, 4};
//        int[] arr2 = {1, 3, 4};
        int[] arr1 = {2};
        int[] arr2 = {1};

        LinkedListSolution.ListNode l1 = l.create(arr1);
        LinkedListSolution.ListNode l2 = l.create(arr2);

        LinkedListSolution.ListNode listNode = l.mergeTwoLists(l1, l2);
        log("list node = " + listNode);
    }

    @Test
    public void yanghui() {
        Solution1 s = new Solution1();
//        List<List<Integer>> generate = s.generate(5);
//        log("generate = " + generate.toString());
        List<Integer> row = s.getRow(3);
        log("row = " + row.toString());
    }

    @Test
    public void climbStairs() {
        AUtil.setLog(this);

        Solution1 s = new Solution1();
        AUtil.printCostInMs(() -> {
            int count = s.climbStairs(44);
            log("count = " + count);
        }, "climbStairs");
    }

    @Test
    public void myPow() {
        AUtil.setLog(this);

        Solution1 s = new Solution1();
        AUtil.printCostInMs(() -> {
//            2.00000 -2147483648
            double count = s.myPow(2.0, 11);
            log("count = " + count);
        }, "myPow");
    }


    @Test
    public void kthGrammar() {
        AUtil.setLog(this);

        Conclusion c = new Conclusion();
        AUtil.printCostInMs(() -> {
            int i = c.kthGrammar(2, 1);
//            int i = c.kthGrammar(4, 5);
//            int i = c.kthGrammar(30, 434991989);
            log("i = " + i);
        }, "kthGrammar");
    }

    @Test
    public void hashSet() {
        MyHashSet hashSet = new MyHashSet();
        hashSet.add(1000000);
        boolean contains = hashSet.contains(1000000);
        log("contains = " + contains);
    }


    @Test
    public void intersect() {
        HashSolution hashSolution = new HashSolution();
        int[] nums1 = {3, 1, 2};
        int[] nums2 = {1, 3};
        int[] intersect = hashSolution.intersect(nums1, nums2);
        log("intersect = " + Arrays.toString(intersect));
    }


    @Test
    public void lengthOfLongestSubstring() {
        HashSolution hashSolution = new HashSolution();
//        String str = "abba";
//        String str = "pwwkew";
        String str = "ggububgvfk";
        int count = hashSolution.lengthOfLongestSubstring(str);
        log("count = " + count);
    }

    @Test
    public void fourSumCount() {
        AUtil.setLog(this);
        int[] nums1 = new int[]{-11, 4, 9, -2, -21, -17, -13, 10, -20, -18, 0, -29, -25, -26, -29, -17, -27, -2, -15, -8, -19, -21, 5, -11, -2, -1, -31, 2, -21, -32, -28, -11, -29, 8, -17, -27, -5, 0, 3, 6, 6, -32, 10, 9, -10, -19, -11, -32, 8, -23, -10, -7, -4, -3, 8, 2, 9, 4, -22, -26, -32, -12, 0, -22, -11, 4, 0, -18, -10, -11, 4, 8, -16, -3, -32, -3, -24, -5, 2, 7, -27, -12, -7, -14, 2, -29, -7, -21, -8, 1, -25, -22, -16, -28, -27, -22, -30, -12, -26, -19};
        int[] nums2 = new int[]{-23, -18, -19, -7, -23, -31, -12, -9, -24, -7, -21, -5, -8, -15, -4, 1, 8, -16, -7, -32, -10, 8, -21, -17, -30, -19, -11, -28, -9, 7, -18, -5, 8, 4, -21, -8, 4, -24, -32, -17, -8, 7, 9, 10, -20, -24, -27, -27, 1, 3, -4, -7, -21, -23, -5, -26, -10, 1, -6, -18, 7, -6, 3, 8, -20, -23, -8, -10, -22, 3, -32, -18, -29, -13, -29, -1, -10, -3, -28, -30, -17, 9, -20, 1, -15, -8, -7, -10, -27, -31, -5, 1, 3, -28, -16, -22, -18, -14, 0, 1};
        int[] nums3 = new int[]{-2, -7, -18, -23, -13, 7, -18, 3, -28, -32, -7, 3, -22, -10, -23, -1, -3, -30, -14, 2, -27, 5, 3, -28, -1, 0, 7, -9, -20, -20, -24, -29, -18, -13, -27, 3, -4, -25, -4, -5, -7, 5, -8, -4, -13, -14, -12, 4, -13, 10, -7, -21, -18, -12, -10, -29, 4, -8, 1, 4, -4, -4, -15, 8, -7, -19, -13, -30, -31, -32, -24, -30, -10, -18, 8, -32, -20, 5, -10, -8, -26, -20, -19, -8, -28, -19, -15, -24, -31, 0, -18, -1, -18, -5, -6, -2, 7, -24, -17, 6};
        int[] nums4 = new int[]{-9, 4, 5, 5, -17, -26, 5, -18, 9, -6, -10, -13, -32, -9, -22, -11, -3, -16, -31, -4, -13, -8, -16, -15, -10, -13, -12, -22, -12, 2, -28, -3, -11, -3, -17, -23, -5, -9, -29, -17, -15, -5, -26, -27, -1, -14, 6, 6, 2, -23, 10, 10, -11, -7, 7, -10, -20, -3, -19, -28, -8, -11, -28, -20, -2, 2, -18, -18, 0, -20, 8, -12, -20, -28, -28, 4, -31, -25, 4, -2, 0, 6, -28, 5, -32, 10, -31, 5, 9, -21, -16, -9, 0, -20, -26, 8, -3, -16, 1, -24};
        HashSolution hashSolution = new HashSolution();
        AUtil.printCostInMs(() -> {
            int count = hashSolution.fourSumCount(nums1, nums2, nums3, nums4);
            log("count = " + count);
        }, "fourSumCount");
    }

    public void log(String msg) {
        log("Exam1", msg);
    }

    @Override
    public void log(String tag, String msg) {
        System.out.println(tag + ": " + msg);
    }
}
